package fr.iut2.tc1.projet.client;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
 * EditSponsor est la classe qui permet de mofifier les 
 * informations liées au sponsors*/
public class EditSponsor extends Composite {
	private HorizontalPanel action;
	private StackPanel modification;
	private Button modifier;
	private Button modifierNom;
	private VerticalPanel changeNom;
	private VerticalPanel changeAdresse;
	private final TextArea nom;
	private final TextArea addresse;
	private final TextArea typedeDon;
	private Label nom1;
	private Label description1;
	private String textStack;
	private Image icon;
	private static ImageBund monBundle;
	private Sponsor monSponsor;
	private final ServicesAsync services = GWT.create(Services.class);
/**
 * Le contructeur creer un menu contistué de deux VerticalPanel un permet de changer
 * le nom du sponsor, l'autre de changer l'addresse*/
	public EditSponsor() {
		modifier = new Button("enregistrer addresse");
		modifierNom=new Button("enregistrer nom");
		action = new HorizontalPanel();
		changeAdresse = new VerticalPanel();
		changeNom = new VerticalPanel();
		monBundle = (ImageBund)GWT.create(ImageBund.class);
		textStack= new String();
		 icon= new Image();
		 //on recupére le sponsor selectionner
		 
		 monSponsor=Joyeuxlurons.getListSponsor().getSelection().getSponsor();
		  
		 icon=monBundle.edit().createImage();
		 //on creer du html pour ajouter l'image a l'ente du stack
		 textStack=Joyeuxlurons.getHeaderString("Modifier nom", icon);

		//pour chaque textArea, un Label permet d'indiquer a l'utilisateur
		//le champs qu'il va modifier
		nom = new TextArea();
		addresse = new TextArea();
		typedeDon = new TextArea();
		nom1 = new Label();
		description1 = new Label();
		modification = new StackPanel();
		
		modification.setVisible(true);
		//on génére le contenu des onglet
		nom1.setText("Nom du sponsor :");
		description1.setText("Adresse du sponsor :");
		nom.setPixelSize(200, 40);
		addresse.setPixelSize(200, 120);
		changeNom.add(nom1);
		changeNom.add(nom);
		changeNom.add(modifierNom);
		changeAdresse.add(description1);
		changeAdresse.add(addresse);
		changeAdresse.add(modifier);
		modification.add(changeNom, textStack,true);
		//on génére le header de modifier addresse
		icon=monBundle.addresse().createImage();
		textStack=Joyeuxlurons.getHeaderString("Modifier addresse", icon);
		modification.add(changeAdresse, textStack,true);
		
		modifier.addClickHandler(new ClickHandler() {

			@Override
			/**sur un clic du bouton de mofdification de l'adresse on vient changer 
			 * le fichier XML*/
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				if(monSponsor instanceof SponsorArgent){
					SponsorArgent temp = new SponsorArgent();
			
					temp.setSponsorArgent(monSponsor.getNom(),monSponsor.getUrlLogo(), addresse.toString(),((SponsorArgent) monSponsor).getMontant());
					
				}
				else
				{
					Sponsor temp= new SponsorLot();
					temp=monSponsor;
					temp.setSponsor(monSponsor.getNom(),monSponsor.getUrlLogo(), addresse.toString());
				}

			}

		});
		
		modifierNom.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if(monSponsor instanceof SponsorArgent){
					System.out.println("entre");
					SponsorArgent temp = new SponsorArgent();
					temp.setSponsorArgent(monSponsor.getNom(),monSponsor.getUrlLogo(), addresse.getText(),((SponsorArgent) monSponsor).getMontant());
					//on efface le fichier sur le serveur et on le recreer avec les infos modifier
					AsyncCallback<Void> callbackSponsorArgent = new AsyncCallback<Void>() {
						
						@Override
						public void onSuccess(Void result) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
						}
					};
					AsyncCallback<Void> callbackajoutSponsor= new AsyncCallback<Void>() {
						
						@Override
						public void onSuccess(Void result) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
						}
					};
					
					services.delSponsorArgent(monSponsor.getNom(), callbackSponsorArgent);
					services.addSponsorArgent((SponsorArgent)temp, callbackajoutSponsor);
					
				}
				else
				{
					Sponsor temp= new SponsorLot();
					System.out.println("entre LOT");
					temp=monSponsor;
					temp.setSponsor(nom.getText(),monSponsor.getUrlLogo(), monSponsor.getAdresse());
					AsyncCallback<Void> callbackSponsorLot = new AsyncCallback<Void>() {
						
						@Override
						public void onSuccess(Void result) {
							// TODO Auto-generated method stub
							System.out.println("del sponsorLot");
						}
						
						@Override
						public void onFailure(Throwable caught) {
							System.out.println("loupé del sponsorLot");
						}
					};
					
					AsyncCallback<Void> callbackajoutSponsorLot= new AsyncCallback<Void>() {
						
						@Override
						public void onSuccess(Void result) {
							// TODO Auto-generated method stub
							System.out.println("sponsor ajouter");
						}
						
						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							System.out.println("sponsor Lot n'est pas ajouter");
						}
					};
					
					services.delSponsorLot(monSponsor.getNom(), callbackSponsorLot);
					services.addSponsorLot((SponsorLot)temp, callbackajoutSponsorLot);
			}
			}

		});
		modification.setSize("70px", "60px");

		initWidget(modification);
	}
	
	  public void finalize()
	     { 
	     }
}
